/***************************************************************************
 *
 * Copyright (c) 2021 ShanghaiTech. All Rights Reserved
 * Author: aqiang <heqh1@shanghaitech.edu.cn> <2021-09-27>
 * @brief live
 * 
 **************************************************************************/
#include <iostream>
#include "mux.h"

namespace fv {

#include <iostream>
#include "mux.h"

Demux::Demux() {

}

Demux::~Demux() {
    avformat_close_input(&ifmt_ctx);
    av_freep(&stream_mapping);	
}

int Demux::demux_init(string InputUrl) {
    int ret, i;
	const char *input_url = InputUrl.c_str();
    if ((ret = avformat_open_input(&ifmt_ctx, input_url, 0, 0)) < 0) {
        fprintf(stderr, "Could not open input file '%s'", input_url);
        exit(1);
    }
    if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0) {
        fprintf(stderr, "Failed to retrieve input stream information");
        exit(1);
    }
    av_dump_format(ifmt_ctx, 0, input_url, 0);
    stream_mapping_size = ifmt_ctx->nb_streams;
    stream_mapping.resize(stream_mapping_size);
    for (i = 0; i < ifmt_ctx->nb_streams; i++) {
        AVStream *out_stream;
        AVStream *in_stream = ifmt_ctx->streams[i];
        AVCodecParameters *in_codecpar = in_stream->codecpar;
        if (in_codecpar->codec_type != AVMEDIA_TYPE_AUDIO &&
            in_codecpar->codec_type != AVMEDIA_TYPE_VIDEO &&
            in_codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE) {
            stream_mapping[i] = -1;
            continue;
        }
        if (in_codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
            audio_stream = i;
        }
        if (in_codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
            video_stream = i;
        }
        stream_mapping[i] = stream_index++;
    }
}

int Demux::get_param(struct remux_param &param) {
	param.ifmt_ctx = ifmt_ctx;
	param.stream_index = stream_index;
	param.stream_mapping_size = stream_mapping_size;	
	param.stream_mapping = stream_mapping;
    param.video_stream = video_stream;
    param.audio_stream = audio_stream;
}

int Demux::demux_packet(int *video_count, vector<AVPacket*> &list_video) {
	int ret = 0;
	AVStream *in_stream, *out_stream;
	AVPacket *packet = av_packet_alloc();

	if (ret = av_read_frame(ifmt_ctx, packet) < 0) {
		av_packet_free(&packet);
		return 1;
	}
	in_stream  = ifmt_ctx->streams[packet->stream_index];
	if (packet->stream_index >= stream_mapping_size ||
		stream_mapping[packet->stream_index] < 0) {
		av_packet_free(&packet);
		return 2;
	}
	if(packet->pts < 0) {
		av_packet_free(&packet);
		return 3;
	}

	list_video.push_back(packet);
	*video_count += 1;
    //cout << "demux " << id << " " << packet->pts << " " << endl;
	// cout << "demux " << packet->pts << " " << list_video[id].back()->pts << endl;
	return 0;
}

int Demux::set_id(const int id) {
    this->id = id;
}

bool Demux::is_end(int64_t pts) {
    if(ifmt_ctx->duration / AV_TIME_BASE == pts / 1000) return true;
    else return false;
}
}